import { Component, OnInit, AfterViewInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NavController, MenuController, LoadingController } from '@ionic/angular';
import { UrlService } from 'src/app/service/url.service';
import { ConfigService } from 'src/app/service/config.service';
import { HttpService } from 'src/app/service/http.service';
import { ToolService } from 'src/app/service/tool.service';
import { PopwinService } from 'src/app/service/popwin.service';
import * as $ from 'jquery';
@Component({
  selector: 'app-regist',
  templateUrl: './regist.page.html',
  styleUrls: ['./regist.page.scss'],
})
export class RegistPage implements OnInit{

  public onRegisterForm: FormGroup;
  private tempImg=ConfigService.SERVER_PATH+UrlService.LOGIN_VCODE+"?sessid="+$("app-root").attr("sessid")+"&t="+ Math.random();
  public codeImg="";
  public errorMsg="";
  constructor(public navCtrl: NavController,public menuCtrl: MenuController,public loadingCtrl: LoadingController, private formBuilder: FormBuilder,private http:HttpService,private popWin:PopwinService) { }

  ngOnInit() {
    if(ConfigService.isAgent==1){
      this.navCtrl.navigateRoot("/registagent");
    }
    this.onRegisterForm = this.formBuilder.group({
      'fullName': [null, Validators.compose([
        Validators.required
      ])],
      'mobile': [null, Validators.compose([
        Validators.required
      ])],
      'password': [null, Validators.compose([
        Validators.required
      ])],
      'repassword': [null, Validators.compose([
        Validators.required
      ])],
      'imgcode': [null, Validators.compose([
        Validators.required
      ])]
    });
    this.codeImg=this.tempImg;
  }

  backClick(){
    this.navCtrl.back();
  }

  onChangeFun(){
      this.errorMsg="";
  }

  async registClick() {
      let that = this;
      if(ToolService.isEmpty(this.onRegisterForm.value.inviteCode)){
        this.onRegisterForm.value.inviteCode="";
      }
      if(this.onRegisterForm.value.password!=this.onRegisterForm.value.repassword){
        that.popWin.toast("两次输入的密码不一致");
        return;
      }
      let obj= "{\"username\":\""+this.onRegisterForm.value.fullName+"\",\"password\":\""+this.onRegisterForm.value.password+"\",\"imgcode\":\""+this.onRegisterForm.value.imgcode+"\",\"mobile\":\""+this.onRegisterForm.value.mobile+"\",\"agent\":\""+ConfigService.isAgent+"\"}";
      console.log(obj);
      this.http.get(UrlService.LOGIN_REGISTER, { "callback": "JSONP_CALLBACK", "data":ToolService.aesEncode(obj) }).subscribe(res => {
        let data = res.json();
        ToolService.setEncrypt(data.encrypt);
        console.log(data);
          if(data.code!=ConfigService.SUCCESS_CODE){
            that.popWin.toast(data.message);
            if(data.code==1004){
              that.errorMsg=data.message;
            }
          }else{
            that.popWin.toast(data.message);
            that.navCtrl.navigateBack("/login");
          }
        }, error => {
           console.log(error);
           // this.popWin.toast(ToolService.newError(error));
        }); 

  }

  // // //
  goToLogin() {
    //this.navCtrl.navigateRoot('/');
  }

  loginPage(){
    this.navCtrl.navigateBack("/login");
  }

  refreshCode(){
      this.codeImg=ConfigService.SERVER_PATH+UrlService.LOGIN_VCODE+"?sessid="+$("app-root").attr("sessid")+"&t="+ Math.random();
      event.stopPropagation();
  }



}
